package com.shz.appletsapi.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shz.appletsapi.common.MineSuccessResult;
import com.shz.appletsapi.common.MineUtils;
import com.shz.appletsapi.common.MinerType;
import com.shz.appletsapi.exception.MineException;
import com.shz.appletsapi.model.Head;
import com.shz.appletsapi.model.Miner;
import com.shz.appletsapi.model.MinerHeartbeat;
import com.shz.appletsapi.model.MinerHeartbeatRequest;
import com.shz.appletsapi.model.MinerRequest;
import com.shz.appletsapi.model.MinerVersionRequest;
import com.shz.appletsapi.model.po.MineAccount;
import com.shz.appletsapi.model.po.MineChannel;
import com.shz.appletsapi.model.po.MineHeartbeatConfig;
import com.shz.appletsapi.model.vo.MinerAccountVo;
import com.shz.appletsapi.model.vo.MinerHeartbeatVo;
import com.shz.appletsapi.prop.ShzWebMsg;
import com.shz.appletsapi.repository.MineChannelRepository;
import com.shz.appletsapi.repository.MineHeartbeatConfigRepository;
import com.shz.appletsapi.service.MineAccountService;
import com.shz.appletsapi.service.MineHeartbeatService;
import com.shz.appletsapi.service.MineInfoService;
import com.shz.appletsapi.service.MineVersionService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/miner")
@Slf4j
public class MinerController {
	
	@Autowired
	private MineAccountService mineAccountService;
	
	@Autowired
	private MineHeartbeatService mineHeartbeatService;
	
	@Autowired
	private MineInfoService mineInfoService;
	
	@Autowired
	private MineVersionService mineVersionService;
	
	@Autowired
	private MineChannelRepository mineChannelRepository;
	
	@Autowired
	private MineHeartbeatConfigRepository mineHeartbeatConfigRepository;
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@PostMapping("/minerReqGetAcc")
	public Head minerReqGetAcc(@RequestBody MinerRequest minerRequest) throws MineException {	
		Miner miner = minerRequest.getDetail();
		if(!MineUtils.resolveHead(minerRequest.getType(),MinerType.MINER_REQ_GET_ACC.getType() )) {
			throw new MineException(minerRequest.getType(),ShzWebMsg.MSG_MINETYPENULL);
		}
		if(!MineUtils.resolveHead(minerRequest.getName(),MinerType.MINER_REQ_GET_ACC.getName())) {	
			throw new MineException(minerRequest.getType(),ShzWebMsg.MSG_MINENAMENULL);
		}	
		if(minerRequest.getSerial() == null) {		
			throw new MineException(minerRequest.getType(),ShzWebMsg.MSG_MINESERIALNULL);
		}
		if(miner.getChannel() == null) {
			throw new MineException(minerRequest.getType(),ShzWebMsg.MSG_CHANNELNULL);
		}
		log.info("设备信息---------------------->"+minerRequest.getDetail());
		log.info("获取账号应答------------------------>");
		MineSuccessResult result =new MineSuccessResult(MinerType.MINER_REQ_GET_ACC_RSP.getType(), MinerType.MINER_REQ_GET_ACC_RSP.getName(), minerRequest.getSerial());
		List<MineAccount> accounts= mineAccountService.findByStatus(1,miner.getChannel());
		if(accounts == null) {
			throw new MineException(minerRequest.getType(),ShzWebMsg.MSG_CHANNELNOT);
		}
		MinerAccountVo minerAccountVo=new MinerAccountVo();
		if(!accounts.isEmpty()) {
			MineAccount mineAccount=accounts.get(0);
			minerAccountVo.setRsp_code(0);
			minerAccountVo.setUsr_acc(mineAccount.getUsr_acc());
			minerAccountVo.setUsr_pwd(mineAccount.getUsr_pwd());
			minerAccountVo.setWallet_addr(mineAccount.getWallet_addr());
			minerAccountVo.setCoin_name(mineAccount.getCoin_name());
			minerAccountVo.setCoin_pool_url(mineAccount.getCoin_pool_url());
			minerAccountVo.setAlg_type(mineAccount.getAlg_type());
		}else {
			minerAccountVo.setRsp_code(1);
		}
		saveMineInfo(minerRequest.getDetail());
		result.setDetail(minerAccountVo);
		return result;
	}
	
	
	@PostMapping("/minerReqHeartbeat")
	public Head minerReqHeartbeat(@RequestBody MinerHeartbeatRequest minerHeartbeatRequest) throws MineException {
		MinerHeartbeat minerHeartbeat = minerHeartbeatRequest.getDetail();
		if(!MineUtils.resolveHead(minerHeartbeatRequest.getType(),MinerType.MINER_REQ_HEARTBEAT.getType() )) {
			throw new MineException(minerHeartbeatRequest.getType(),ShzWebMsg.MSG_MINETYPENULL);
		}
		if(!MineUtils.resolveHead(minerHeartbeatRequest.getName(),MinerType.MINER_REQ_HEARTBEAT.getName())) {	
			throw new MineException(minerHeartbeatRequest.getType(),ShzWebMsg.MSG_MINENAMENULL);
		}	
		if(minerHeartbeatRequest.getSerial() == null) {		
			throw new MineException(minerHeartbeatRequest.getType(),ShzWebMsg.MSG_MINESERIALNULL);
		}
		if(minerHeartbeat.getChannel() == null) {
			throw new MineException(minerHeartbeatRequest.getType(),ShzWebMsg.MSG_CHANNELNULL);
		}
		
		MineSuccessResult result =new MineSuccessResult(MinerType.MINER_REQ_HEARTBEAT_RSP.getType(), MinerType.MINER_REQ_HEARTBEAT_RSP.getName(), minerHeartbeatRequest.getSerial());
		log.info("设备心跳信息---------------------->"+minerHeartbeatRequest.getDetail());
		log.info("心跳应答------------------------>");
		
		MineChannel channel= mineChannelRepository.findByName(minerHeartbeat.getChannel());
		if(channel == null) {
			throw new MineException(minerHeartbeatRequest.getType(),ShzWebMsg.MSG_CHANNELNOT);
		}
		MineHeartbeatConfig mineHeartbeatConfig=mineHeartbeatConfigRepository.findByChannelId(channel.getId());
		MinerHeartbeatVo minerHeartbeatVo = new MinerHeartbeatVo();
		minerHeartbeatVo.setTsid(minerHeartbeat.getTsid());
		minerHeartbeatVo.setIs_update(mineVersionService.isVersionUpdate(minerHeartbeat.getMiner_version()) ? 1 : 0);
		if(mineHeartbeatConfig != null) {
			minerHeartbeatVo.setMiner_cmd(mineHeartbeatConfig.getMiner_cmd());
			minerHeartbeatVo.setIs_change_acc(mineHeartbeatConfig.getIs_change_acc());
			minerHeartbeatVo.setDiff(mineHeartbeatConfig.getDiff());
			if(mineHeartbeatConfig.getIs_change_acc()==1 || 
			   mineHeartbeatConfig.getDiff() != 0 || mineHeartbeatConfig.getMiner_cmd() != null) {
			    mineHeartbeatConfig.setIs_change_acc(0);
			    mineHeartbeatConfig.setDiff(0);
			    mineHeartbeatConfig.setMiner_cmd(null);
				mineHeartbeatConfigRepository.save(mineHeartbeatConfig);
			}	
		}
		result.setDetail(minerHeartbeatVo);
		saveMineHeartbeat(minerHeartbeat);
		mineHeartbeatService.realTimeCache(minerHeartbeat.getCurr_time(),minerHeartbeat.getTsid(),minerHeartbeat.getDiff());
		return result;
	}
	
	@PostMapping("/minerReqVersion")
	public Head minerReqVersion(@RequestBody MinerVersionRequest minerVersionRequest) throws MineException {
		if(!MineUtils.resolveHead(minerVersionRequest.getType(),MinerType.MINER_REQ_VERSION.getType() )) {
			throw new MineException(minerVersionRequest.getType(),ShzWebMsg.MSG_MINETYPENULL);
		}
		if(!MineUtils.resolveHead(minerVersionRequest.getName(),MinerType.MINER_REQ_VERSION.getName())) {	
			throw new MineException(minerVersionRequest.getType(),ShzWebMsg.MSG_MINENAMENULL);
		}	
		if(minerVersionRequest.getSerial() == null) {		
			throw new MineException(minerVersionRequest.getType(),ShzWebMsg.MSG_MINESERIALNULL);
		}
		log.info("获取版本应答------------------------>");
		MineSuccessResult result =new MineSuccessResult(MinerType.MINER_REQ_VERSION_RSP.getType(), MinerType.MINER_REQ_VERSION_RSP.getName(), minerVersionRequest.getSerial());
		result.setDetail(mineVersionService.findVersion(minerVersionRequest.getDetail().getTsid()));
		return result;
	}
	
	
	
	
	private void saveMineInfo(Miner miner) {
		mineInfoService.saveMineInfo(miner);		
	}
	
	private void saveMineHeartbeat(MinerHeartbeat minerHeartbeat) {
		mineHeartbeatService.saveMineHeartbeat(minerHeartbeat);	
		mineHeartbeatService.saveMineHeartbeatRealtime(minerHeartbeat);
	}
	
	
	
	

	

}
